/*
 * @Description: 新建页面按钮
 */
import { defineStore } from 'pinia';
import globalData from '@/plugins/data';
import api from '@/api';
//新建页面静态按钮
import staticBtnList, { print, deleteDraft } from './static-btn-list';
//格式化数据
import formatter from './formatter.js';
//展示独立按钮的数量，超过的加入下拉选择
const SHOW_BTN_NUMBER = 3;
//按钮的显示排序
export const BTN_ORDER_LIST=['send','saveWaitSend','callTemplate','saveAsTemplate','newDoPrint','deleteDraft'];
export const useNewButtonStore = defineStore('newButton', {
  state: () => ({
    //是否是简单布局页面
    isSimple: false,
    //按钮列表
    btnList: [],
    //下拉列表(简单布局页面、处理页面-高级操作)
    selectList: [],
    //是否展示正文类型按钮
    isShowBodyTypeBtn: false,
  }),
  actions: {
    //获取按钮相关数据
    async getNewButton(type, params) {
      const data = await api.btnGroupTopGetButtonList(params);
      const list = formatter(data.baseActionList);
      console.log(['staticList', list]);
      this.btnList=[...list];
      //排序
      this.btnList.sort((a,b)=>{
        let indexA = BTN_ORDER_LIST.indexOf(a.handle);
        let indexB = BTN_ORDER_LIST.indexOf(b.handle);

        if (indexA === -1 && indexB === -1) {
          return this.btnList.indexOf(a) - this.btnList.indexOf(b);
        } else if (indexA === -1) {
          return 1;
        } else if (indexB === -1) {
          return -1;
        } else {
          return indexA - indexB;
        }
      });
      //如果展示打印按钮
      if (data.canPrint) {
        this.btnList.push(print);
      }
      //如果展示“待办不显示按钮”(删除)
      if (globalData.openFrom === 'listPending') {
        this.btnList.push(deleteDraft);
      }
      //是否展示正文类型
      this.isShowBodyTypeBtn = !data.hideContent;

      //加loading
      list.forEach((d) => {
        d.loading = false;
      });

      // //根据模板不同，按钮展示方式也不同
      // switch (type) {
      //     case 'simple':
      //         this.isSimple = true;
      //         //简单模板，多余的按钮加入下拉选择
      //         this.selectList = finallyList.splice(SHOW_BTN_NUMBER);
      //     default:
      // }
      this.selectList = list.slice();
    },
  },
});
